<template>
  <a-modal
    :maskClosable="false"
    :keyboard="false"
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label="$t('m.system.job.entity.jobName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'jobName', validatorRules.jobName]" placeholder=""></a-input>
        </a-form-item>

        <a-form-item :label="$t('m.system.job.entity.jobCode')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'jobCode', validatorRules.jobCode]" placeholder=""></a-input>
        </a-form-item>

        <a-form-item :label="$t('m.system.job.entity.jobOrder')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :min="0" :max="999999999" v-decorator="[ 'jobOrder',{'initialValue':0}]" />
        </a-form-item>

        <a-form-item :label="$t('m.system.job.entity.description')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['description']" rows="4" placeholder=""/>
        </a-form-item>


      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";
  import { duplicateCheck } from '@/api/api'

  export default {
    name: "SysJobModal",
    components: {
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        jobName:{rules: [
            { required: true, message: this.$t('m.system.job.verify.jobName') },
            { validator: this.validateJobName }
          ]},
        jobCode:{rules: [
            { required: true, message: this.$t('m.system.job.verify.jobCode')  },
            { validator: this.validateJobCode }
            ]},
        jobOrder:{},
        description:{},
        },
        url: {
          add: YchConstant.PREFIX_SYSTEM + "/sysJob/add",
          edit: YchConstant.PREFIX_SYSTEM + "/sysJob/edit",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(this.lodash.pick(this.model,'jobName','jobCode','jobOrder','description'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            ////console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.messageUtil.success(res.message);
                that.$emit('ok');
              }else{
                that.messageUtil.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(this.lodash.pick(row,'jobName','jobCode','jobOrder','description'))
      },
      validateJobName(rule, value, callback) {
        var params = {
          tableName: 'sys_job',
          fieldName: 'job_name',
          fieldVal: value,
          dataId: this.model.id,
        }
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback(this.$t('m.system.job.verify.jobName_repeat'))
          }
        })
      },
      validateJobCode(rule, value, callback) {
        var params = {
          tableName: 'sys_job',
          fieldName: 'job_code',
          fieldVal: value,
          dataId: this.model.id,
        }
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback(this.$t('m.system.job.verify.jobCode_repeat'))
          }
        })
      },

    }
  }
</script>
